package com.bzy.common.manager;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.UUID;
import com.bzy.common.util.LoggerUtil;
import com.bzy.shiro.Constants;
import org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.ScanParams;
import redis.clients.jedis.ScanResult;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;

public class JedisManager {

	private ShardedJedisPool shardedJedisPool;
	

	public void setShardedJedisPool(ShardedJedisPool shardedJedisPool) {
		this.shardedJedisPool = shardedJedisPool;
	}

	public Set<byte[]> keys(final byte[] pattern) {
		Set<byte[]> result = null;
		ShardedJedis shardedJedis = null;
		try {
			shardedJedis = shardedJedisPool.getResource();
			Iterator<Jedis> jedisIterator = shardedJedis.getAllShards().iterator();
			while (jedisIterator.hasNext()) {
				Set<byte[]> part = jedisIterator.next().keys(pattern);
				if (result == null) {
					result = part;
				} else {
					result.addAll(part);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			LoggerUtil.redisLog("set error.", e);
		} finally {
			if (shardedJedis != null) {
				shardedJedis.close();
			}
		}
		return result;
	}

	public boolean set(byte[] key, byte[] value, int expireTime) {
		boolean result = false;
		ShardedJedis shardedJedis = null;
		try {
			shardedJedis = shardedJedisPool.getResource();
			shardedJedis.set(key, value);
			if (expireTime > 0) {
				shardedJedis.expire(key, expireTime);
			}
			result = true;
		} catch (Exception e) {
			e.printStackTrace();
			LoggerUtil.redisLog("set error.", e);
		} finally {
			if (shardedJedis != null) {
				shardedJedis.close();
			}
		}
		return result;
	}

	public byte[] get(byte[] key) {
		byte[] result = null;
		ShardedJedis shardedJedis = null;
		try {
			shardedJedis = shardedJedisPool.getResource();
			result = shardedJedis.get(key);
		} catch (Exception e) {
			e.printStackTrace();
			LoggerUtil.redisLog("get error.", e);
		} finally {
			if (shardedJedis != null) {
				shardedJedis.close();
			}
		}
		return result;
	}

	public boolean del(byte[] key) {
		boolean result = false;
		ShardedJedis shardedJedis = null;
		try {
			shardedJedis = shardedJedisPool.getResource();
			shardedJedis.del(key);
			result = true;
		} catch (Exception e) {
			e.printStackTrace();
			LoggerUtil.redisLog("del error.", e);
		} finally {
			if (shardedJedis != null) {
				shardedJedis.close();
			}
		}
		return result;
	}

	public boolean set(String key, String value, int second) {
		boolean result = false;
		ShardedJedis shardedJedis = null;
		try {
			shardedJedis = shardedJedisPool.getResource();


			// 是否为密码认证错误匹配（暂时底层判断处理，后期改为ehcache缓存来处理）
			if (key.indexOf(Constants.REDIS_SHIRO_CACHE_RETRYCACHE) > 0) {

				shardedJedis.setex(key, 600, value);

			} else { // 非密码认证缓存

				if (second > 0) {
					shardedJedis.setex(key, second, value);
				} else {
					shardedJedis.set(key, value);
				}
			}
			result = true;
		} catch (Exception e) {
			e.printStackTrace();
			LoggerUtil.redisLog("set error.", e);
		} finally {
			if (shardedJedis != null) {
				shardedJedis.close();
			}
		}
		return result;
	}

	public boolean set(String key, String value) {
		boolean result = false;
		ShardedJedis shardedJedis = null;
		try {
			shardedJedis = shardedJedisPool.getResource();
			shardedJedis.set(key, value);
			result = true;
		} catch (Exception e) {
			e.printStackTrace();
			LoggerUtil.redisLog("set error.", e);
		} finally {
			if (shardedJedis != null) {
				shardedJedis.close();
			}
		}
		return result;
	}

	public String get(String key, String defaultValue) {
		String result = defaultValue;
		ShardedJedis shardedJedis = null;
		try {
			shardedJedis = shardedJedisPool.getResource();
			result = shardedJedis.get(key) == null ? defaultValue : shardedJedis.get(key);
		} catch (Exception e) {
			e.printStackTrace();
			LoggerUtil.redisLog("get error.", e);
		} finally {
			if (shardedJedis != null) {
				shardedJedis.close();
			}
		}
		return result;
	}

	public boolean del(String key) {
		boolean result = false;
		ShardedJedis shardedJedis = null;
		try {
			shardedJedis = shardedJedisPool.getResource();
			shardedJedis.del(key);
			result = true;
		} catch (Exception e) {
			e.printStackTrace();
			LoggerUtil.redisLog("del error.", e);
		} finally {
			if (shardedJedis != null) {
				shardedJedis.close();
			}
		}
		return result;
	}

	/**
	 * 设置一个key的过期时间（单位：秒）
	 * 
	 * @param key
	 *            key值
	 * @param seconds
	 *            多少秒后过期
	 * @return 1：设置了过期时间 0：没有设置过期时间/不能设置过期时间
	 */
	public long expire(String key, int seconds) {
		if (key == null || key.equals("")) {
			return 0;
		}

		long result = 0;
		ShardedJedis shardedJedis = null;
		try {
			shardedJedis = shardedJedisPool.getResource();
			result = shardedJedis.expire(key, seconds);
		} catch (Exception e) {
			e.printStackTrace();
			LoggerUtil.redisLog("EXPIRE error[key=" + key + " seconds=" + seconds + "]" + e.getMessage(), e);
		} finally {
			if (shardedJedis != null) {
				shardedJedis.close();
			}
		}
		return result;
	}

	/**
	 * 设置一个key在某个时间点过期
	 * 
	 * @param key
	 *            key值
	 * @param unixTimestamp
	 *            unix时间戳，从1970-01-01 00:00:00开始到现在的秒数
	 * @return 1：设置了过期时间 0：没有设置过期时间/不能设置过期时间
	 */
	public long expireAt(String key, int unixTimestamp) {
		if (key == null || key.equals("")) {
			return 0;
		}

		long result = 0;
		ShardedJedis shardedJedis = null;
		try {
			shardedJedis = shardedJedisPool.getResource();
			result = shardedJedis.expireAt(key, unixTimestamp);
		} catch (Exception e) {
			e.printStackTrace();
			LoggerUtil.redisLog("EXPIRE error[key=" + key + " unixTimestamp=" + unixTimestamp + "]" + e.getMessage(),
					e);
		} finally {
			if (shardedJedis != null) {
				shardedJedis.close();
			}
		}
		return result;
	}

	/* 将 key 中储存的数字值增一，如果 key 不存在，那么 key 的值会先被初始化为 0 ，然后再执行 INCR 操作 */
	public long incr(String key) {
		long result = 0;
		ShardedJedis shardedJedis = null;
		try {
			shardedJedis = shardedJedisPool.getResource();
			result = shardedJedis.incr(key);
		} catch (Exception e) {
			e.printStackTrace();
			LoggerUtil.redisLog("incr error.", e);
		} finally {
			if (shardedJedis != null) {
				shardedJedis.close();
			}
		}
		return result;
	}

	public long decr(String key) {
		long result = 0;
		ShardedJedis shardedJedis = null;
		try {
			shardedJedis = shardedJedisPool.getResource();
			result = shardedJedis.decr(key);
		} catch (Exception e) {
			e.printStackTrace();
			LoggerUtil.redisLog("incr error.", e);
		} finally {
			if (shardedJedis != null) {
				shardedJedis.close();
			}
		}
		return result;
	}

	/**
	 * 截断一个List
	 * 
	 * @param key
	 *            列表key
	 * @param start
	 *            开始位置 从0开始
	 * @param end
	 *            结束位置
	 * @return 状态码
	 */
	public String trimList(String key, long start, long end) {
		if (key == null || key.equals("")) {
			return "-";
		}

		String result = "-";
		ShardedJedis shardedJedis = null;
		try {
			shardedJedis = shardedJedisPool.getResource();
			result = shardedJedis.ltrim(key, start, end);
		} catch (Exception e) {
			e.printStackTrace();
			LoggerUtil.redisLog("LTRIM 出错[key=" + key + " start=" + start + " end=" + end + "]" + e.getMessage(), e);
		} finally {
			if (shardedJedis != null) {
				shardedJedis.close();
			}
		}
		return result;
	}

	/**
	 * 检查Set长度
	 * 
	 * @param key
	 * @return
	 */
	public long countSet(String key) {
		if (key == null) {
			return 0;
		}

		long result = 0;
		ShardedJedis shardedJedis = null;
		try {
			shardedJedis = shardedJedisPool.getResource();
			result = shardedJedis.scard(key);
		} catch (Exception e) {
			e.printStackTrace();
			LoggerUtil.redisLog("countSet error.", e);
		} finally {
			if (shardedJedis != null) {
				shardedJedis.close();
			}
		}
		return result;
	}

	public void sadd(String key, Set<String> set, int seconds) {
		for (String value : set) {
			sadd(key, value);
		}
		if (seconds > 0) {
			expire(key, seconds);
		}
	}

	/**
	 * 添加到Set中（同时设置过期时间）
	 * 
	 * @param key
	 *            key值
	 * @param seconds
	 *            过期时间 单位s
	 * @param value
	 * @return
	 */
	public boolean sadd(String key, int seconds, String... value) {
		boolean result = sadd(key, value);
		if (result) {
			long i = expire(key, seconds);
			return i == 1;
		}
		return false;
	}

	/**
	 * 添加到Set中
	 * 
	 * @param key
	 * @param value
	 * @return
	 */
	public boolean sadd(String key, String... value) {
		if (key == null || value == null) {
			return false;
		}

		boolean result = false;
		ShardedJedis shardedJedis = null;
		try {
			shardedJedis = shardedJedisPool.getResource();
			shardedJedis.sadd(key, value);
			result = true;
		} catch (Exception e) {
			e.printStackTrace();
			LoggerUtil.redisLog("setList error.", e);
		} finally {
			if (shardedJedis != null) {
				shardedJedis.close();
			}
		}
		return result;
	}

	/**
	 * @param key
	 * @param value
	 * @return 判断值是否包含在set中
	 */
	public boolean sismember(String key, String value) {
		if (key == null || value == null) {
			return false;
		}

		boolean result = false;
		ShardedJedis shardedJedis = null;
		try {
			shardedJedis = shardedJedisPool.getResource();
			result = shardedJedis.sismember(key, value);
		} catch (Exception e) {
			e.printStackTrace();
			LoggerUtil.redisLog("setList error.", e);
		} finally {
			if (shardedJedis != null) {
				shardedJedis.close();
			}
		}
		return result;
	}

	/**
	 * 获取Set
	 * 
	 * @param key
	 * @return
	 */
	public Set<String> smembers(String key) {
		Set<String> result = null;
		ShardedJedis shardedJedis = null;
		try {
			shardedJedis = shardedJedisPool.getResource();
			result = shardedJedis.smembers(key);
		} catch (Exception e) {
			e.printStackTrace();
			LoggerUtil.redisLog("getList error.", e);
		} finally {
			if (shardedJedis != null) {
				shardedJedis.close();
			}
		}
		return result;
	}

	/**
	 * 从set中删除value
	 * 
	 * @param key
	 * @return
	 */
	public boolean srem(String key, String... value) {
		boolean result = false;
		ShardedJedis shardedJedis = null;
		try {
			shardedJedis = shardedJedisPool.getResource();
			shardedJedis.srem(key, value);
			result = true;
		} catch (Exception e) {
			e.printStackTrace();
			LoggerUtil.redisLog("getList error.", e);
		} finally {
			if (shardedJedis != null) {
				shardedJedis.close();
			}
		}
		return result;
	}

	/**
	 * 从list中删除value 默认count 1
	 * 
	 * @param key
	 * @param values
	 *            值list
	 * @return
	 */
	public int removeListValue(String key, List<String> values) {
		return removeListValue(key, 1, values);
	}

	/**
	 * 从list中删除value
	 * 
	 * @param key
	 * @param count
	 * @param values
	 *            值list
	 * @return
	 */
	public int removeListValue(String key, long count, List<String> values) {
		int result = 0;
		if (values != null && values.size() > 0) {
			for (String value : values) {
				if (removeListValue(key, count, value)) {
					result++;
				}
			}
		}
		return result;
	}

	/**
	 * 从list中删除value
	 * 
	 * @param key
	 * @param count
	 *            要删除个数
	 * @param value
	 * @return
	 */
	public boolean removeListValue(String key, long count, String value) {
		boolean result = false;
		ShardedJedis shardedJedis = null;
		try {
			shardedJedis = shardedJedisPool.getResource();
			shardedJedis.lrem(key, count, value);
			result = true;
		} catch (Exception e) {
			e.printStackTrace();
			LoggerUtil.redisLog("getList error.", e);
		} finally {
			if (shardedJedis != null) {
				shardedJedis.close();
			}
		}
		return result;
	}

	/**
	 * 截取List
	 * 
	 * @param key
	 * @param start
	 *            起始位置
	 * @param end
	 *            结束位置
	 * @return
	 */
	public List<String> rangeList(String key, long start, long end) {
		if (key == null || key.equals("")) {
			return null;
		}

		List<String> result = null;
		ShardedJedis shardedJedis = null;
		try {
			shardedJedis = shardedJedisPool.getResource();
			result = shardedJedis.lrange(key, start, end);
		} catch (Exception e) {
			e.printStackTrace();
			LoggerUtil.redisLog("rangeList 出错[key=" + key + " start=" + start + " end=" + end + "]" + e.getMessage(),
					e);
		} finally {
			if (shardedJedis != null) {
				shardedJedis.close();
			}
		}
		return result;
	}

	/**
	 * 检查List长度
	 * 
	 * @param key
	 * @return
	 */
	public long countList(String key) {
		if (key == null) {
			return 0;
		}

		long result = 0;
		ShardedJedis shardedJedis = null;
		try {
			shardedJedis = shardedJedisPool.getResource();
			result = shardedJedis.llen(key);
		} catch (Exception e) {
			e.printStackTrace();
			LoggerUtil.redisLog("countList error.", e);
		} finally {
			if (shardedJedis != null) {
				shardedJedis.close();
			}
		}
		return result;
	}

	/**
	 * 添加到List中（同时设置过期时间）
	 * 
	 * @param key
	 *            key值
	 * @param seconds
	 *            过期时间 单位s
	 * @param value
	 * @return
	 */
	public boolean lpush(String key, int seconds, String... value) {
		boolean result = lpush(key, value);
		if (result) {
			long i = expire(key, seconds);
			return i == 1;
		}
		return false;
	}

	/**
	 * 添加到List
	 * 
	 * @param key
	 * @param value
	 * @return
	 */
	public boolean lpush(String key, String... value) {
		if (key == null || value == null) {
			return false;
		}

		boolean result = false;
		ShardedJedis shardedJedis = null;
		try {
			shardedJedis = shardedJedisPool.getResource();
			shardedJedis.lpush(key, value);
			result = true;
		} catch (Exception e) {
			e.printStackTrace();
			LoggerUtil.redisLog("setList error.", e);
		} finally {
			if (shardedJedis != null) {
				shardedJedis.close();
			}
		}
		return result;
	}

	/**
	 * 添加到List(只新增)
	 * 
	 * @param key
	 * @param list
	 * @return
	 */
	public boolean lpush(String key, List<String> list) {
		if (key == null || list == null || list.size() == 0) {
			return false;
		}
		for (String value : list) {
			lpush(key, value);
		}
		return true;
	}

	/**
	 * 获取List
	 * 
	 * @param key
	 * @return
	 */
	public List<String> getList(String key) {
		List<String> result = null;
		ShardedJedis shardedJedis = null;
		try {
			shardedJedis = shardedJedisPool.getResource();
			result = shardedJedis.lrange(key, 0, -1);
		} catch (Exception e) {
			e.printStackTrace();
			LoggerUtil.redisLog("getList error.", e);
		} finally {
			if (shardedJedis != null) {
				shardedJedis.close();
			}
		}
		return result;
	}

	/**
	 * 设置HashSet对象
	 * 
	 * @param domain
	 *            域名
	 * @param key
	 *            键值
	 * @param value
	 *            Json String or String value
	 * @return
	 */
	public boolean setHSet(String domain, String key, String value) {
		if (value == null)
			return false;

		boolean result = false;
		ShardedJedis shardedJedis = null;
		try {
			shardedJedis = shardedJedisPool.getResource();
			shardedJedis.hset(domain, key, value);
			result = true;
		} catch (Exception e) {
			e.printStackTrace();
			LoggerUtil.redisLog("setHSet error.", e);
		} finally {
			if (shardedJedis != null) {
				shardedJedis.close();
			}
		}
		return result;
	}

	/**
	 * 获得HashSet对象
	 * 
	 * @param domain
	 *            域名
	 * @param key
	 *            键值
	 * @return Json String or String value
	 */
	public String getHSet(String domain, String key) {
		String result = null;
		ShardedJedis shardedJedis = null;
		try {
			shardedJedis = shardedJedisPool.getResource();
			result = shardedJedis.hget(domain, key);
		} catch (Exception e) {
			e.printStackTrace();
			LoggerUtil.redisLog("getHSet error.", e);
		} finally {
			if (shardedJedis != null) {
				shardedJedis.close();
			}
		}
		return result;
	}

	/**
	 * 删除HashSet对象
	 * 
	 * @param domain
	 *            域名
	 * @param key
	 *            键值
	 * @return 删除的记录数
	 */
	public long delHSet(String domain, String key) {
		ShardedJedis shardedJedis = null;
		long result = 0;
		try {
			shardedJedis = shardedJedisPool.getResource();
			result = shardedJedis.hdel(domain, key);
		} catch (Exception e) {
			e.printStackTrace();
			LoggerUtil.redisLog("delHSet error.", e);
		} finally {
			if (shardedJedis != null) {
				shardedJedis.close();
			}
		}
		return result;
	}

	/**
	 * 删除HashSet对象
	 * 
	 * @param domain
	 *            域名
	 * @param key
	 *            键值
	 * @return 删除的记录数
	 */
	public long delHSet(String domain, String... key) {
		ShardedJedis shardedJedis = null;
		long result = 0;
		try {
			shardedJedis = shardedJedisPool.getResource();
			result = shardedJedis.hdel(domain, key);
		} catch (Exception e) {
			e.printStackTrace();
			LoggerUtil.redisLog("delHSet error.", e);
		} finally {
			if (shardedJedis != null) {
				shardedJedis.close();
			}
		}
		return result;
	}

	/**
	 * 判断key是否存在
	 * 
	 * @param domain
	 *            域名
	 * @param key
	 *            键值
	 * @return
	 */
	public boolean existsHSet(String domain, String key) {
		ShardedJedis shardedJedis = null;
		boolean result = false;
		try {
			shardedJedis = shardedJedisPool.getResource();
			result = shardedJedis.hexists(domain, key);
		} catch (Exception e) {
			e.printStackTrace();
			LoggerUtil.redisLog("existsHSet error.", e);
		} finally {
			if (shardedJedis != null) {
				shardedJedis.close();
			}
		}
		return result;
	}

	/**
	 * 全局扫描hset
	 * 
	 * @param match
	 *            field匹配模式
	 * @return
	 */
	public List<Map.Entry<String, String>> scanHSet(String domain, String match) {
		List<Map.Entry<String, String>> result = null;
		ShardedJedis shardedJedis = null;
		try {
			int cursor = 0;
			shardedJedis = shardedJedisPool.getResource();
			ScanParams scanParams = new ScanParams();
			scanParams.match(match);
			Jedis jedis = shardedJedis.getShard(domain);
			ScanResult<Map.Entry<String, String>> scanResult;
			List<Map.Entry<String, String>> list = new ArrayList<Map.Entry<String, String>>();
			do {
				scanResult = jedis.hscan(domain, String.valueOf(cursor), scanParams);
				list.addAll(scanResult.getResult());
				cursor = Integer.parseInt(scanResult.getStringCursor());
			} while (cursor > 0);
			result = list;
		} catch (Exception e) {
			e.printStackTrace();
			LoggerUtil.redisLog("scanHSet error.", e);
		} finally {
			if (shardedJedis != null) {
				shardedJedis.close();
			}
		}
		return result;
	}

	/**
	 * 返回 domain 指定的哈希集中所有字段的value值
	 * 
	 * @param domain
	 * @return
	 */

	public List<String> hvals(String domain) {
		ShardedJedis shardedJedis = null;
		List<String> result = null;
		try {
			shardedJedis = shardedJedisPool.getResource();
			result = shardedJedis.hvals(domain);
		} catch (Exception e) {
			e.printStackTrace();
			LoggerUtil.redisLog("hvals error.", e);
		} finally {
			if (shardedJedis != null) {
				shardedJedis.close();
			}
		}
		return result;
	}

	/**
	 * 返回 domain 指定的哈希集中所有字段的key值
	 * 
	 * @param domain
	 * @return
	 */

	public Set<String> hkeys(String domain) {
		ShardedJedis shardedJedis = null;
		Set<String> result = null;
		try {
			shardedJedis = shardedJedisPool.getResource();
			result = shardedJedis.hkeys(domain);
		} catch (Exception e) {
			e.printStackTrace();
			LoggerUtil.redisLog("hkeys error.", e);
		} finally {
			if (shardedJedis != null) {
				shardedJedis.close();
			}
		}
		return result;
	}

	/**
	 * 返回 domain 指定的哈希key值总数
	 * 
	 * @param domain
	 * @return
	 */
	public long lenHset(String domain) {
		ShardedJedis shardedJedis = null;
		long result = 0;
		try {
			shardedJedis = shardedJedisPool.getResource();
			result = shardedJedis.hlen(domain);
		} catch (Exception e) {
			e.printStackTrace();
			LoggerUtil.redisLog("hkeys error.", e);
		} finally {
			if (shardedJedis != null) {
				shardedJedis.close();
			}
		}
		return result;
	}

	/**
	 * 设置排序集合
	 * 
	 * @param key
	 * @param score
	 * @param value
	 * @return
	 */
	public boolean setSortedSet(String key, long score, String value) {
		boolean result = false;
		ShardedJedis shardedJedis = null;
		try {
			shardedJedis = shardedJedisPool.getResource();
			shardedJedis.zadd(key, score, value);
			result = true;
		} catch (Exception e) {
			e.printStackTrace();
			LoggerUtil.redisLog("setSortedSet error.", e);
		} finally {
			if (shardedJedis != null) {
				shardedJedis.close();
			}
		}
		return result;
	}

	/**
	 * 获得排序集合
	 * 
	 * @param key
	 * @param startScore
	 * @param endScore
	 * @param orderByDesc
	 * @return
	 */
	public Set<String> getSoredSet(String key, long startScore, long endScore, boolean orderByDesc) {
		Set<String> result = null;
		ShardedJedis shardedJedis = null;
		try {
			shardedJedis = shardedJedisPool.getResource();
			if (orderByDesc) {
				result = shardedJedis.zrevrangeByScore(key, endScore, startScore);
			} else {
				result = shardedJedis.zrangeByScore(key, startScore, endScore);
			}
		} catch (Exception e) {
			e.printStackTrace();
			LoggerUtil.redisLog("getSoredSet error.", e);
		} finally {
			if (shardedJedis != null) {
				shardedJedis.close();
			}
		}
		return result;
	}

	/**
	 * 计算排序长度
	 * 
	 * @param key
	 * @param startScore
	 * @param endScore
	 * @return
	 */
	public long countSoredSet(String key, long startScore, long endScore) {
		long result = 0L;
		ShardedJedis shardedJedis = null;
		try {
			shardedJedis = shardedJedisPool.getResource();
			Long count = shardedJedis.zcount(key, startScore, endScore);
			result = count == null ? 0L : count;
		} catch (Exception e) {
			e.printStackTrace();
			LoggerUtil.redisLog("countSoredSet error.", e);
		} finally {
			if (shardedJedis != null) {
				shardedJedis.close();
			}
		}
		return result;
	}

	/**
	 * 删除排序集合
	 * 
	 * @param key
	 * @param value
	 * @return
	 */
	public boolean delSortedSet(String key, String value) {
		boolean result = false;
		ShardedJedis shardedJedis = null;
		try {
			shardedJedis = shardedJedisPool.getResource();
			long count = shardedJedis.zrem(key, value);
			result = count > 0;
		} catch (Exception e) {
			e.printStackTrace();
			LoggerUtil.redisLog("delSortedSet error.", e);
		} finally {
			if (shardedJedis != null) {
				shardedJedis.close();
			}
		}
		return result;
	}

	/**
	 * 获得排序集合
	 * 
	 * @param key
	 * @param startRange
	 * @param endRange
	 * @param orderByDesc
	 * @return
	 */
	public Set<String> getSoredSetByRange(String key, int startRange, int endRange, boolean orderByDesc) {
		Set<String> result = null;
		ShardedJedis shardedJedis = null;
		try {
			shardedJedis = shardedJedisPool.getResource();
			if (orderByDesc) {
				result = shardedJedis.zrevrange(key, startRange, endRange);
			} else {
				result = shardedJedis.zrange(key, startRange, endRange);
			}
		} catch (Exception e) {
			e.printStackTrace();
			LoggerUtil.redisLog("getSoredSetByRange error.", e);
		} finally {
			if (shardedJedis != null) {
				shardedJedis.close();
			}
		}
		return result;
	}

	/**
	 * 获得排序打分
	 * 
	 * @param key
	 * @return
	 */
	public Double getScore(String key, String member) {
		Double result = null;
		ShardedJedis shardedJedis = null;
		try {
			shardedJedis = shardedJedisPool.getResource();
			result = shardedJedis.zscore(key, member);
		} catch (Exception e) {
			e.printStackTrace();
			LoggerUtil.redisLog("getSoredSet error.", e);
		} finally {
			if (shardedJedis != null) {
				shardedJedis.close();
			}
		}
		return result;
	}

	

	/**
	 * 尝试获取分布式锁
	 * @param lockKey
	 * 		要锁住的 key 
	 * @param timeout
	 * 		超时处理时间 mills
	 * @param expireTime
	 * 		键过期时间 seconds
	 * @return 锁标识
	 */
	public String getLock(String lockKey, long timeout, int expireTime) {
		ShardedJedis shardedJedis = null;
		try {
			shardedJedis = shardedJedisPool.getResource();
			String value = UUID.randomUUID().toString();
			long end = System.currentTimeMillis() + timeout;
			while(System.currentTimeMillis() < end) { //阻塞
				String result = shardedJedis.set(lockKey, value, "NX", "PX", expireTime*1000L);
				if (result != null && result.equals("OK")) {
					// 锁设置成功，redis操作成功
					return value;
				}
				// 检测过期时间
                // 如果键没有关联的过期，则返回 -1
                if(shardedJedis.ttl(lockKey) == -1){ 
                	shardedJedis.expire(lockKey,expireTime);
                }
                Thread.sleep(800);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		} 
		return null;
	}
	
	
	/**
	 * 释放redis锁（删除）
	 * @param lockKey
	 * @param value
	 * @return
	 */
	public boolean releaseLock(String lockKey, String value) {
		ShardedJedis shardedJedis = null;
		try {
			shardedJedis = shardedJedisPool.getResource();
			String cacheVal = shardedJedis.get(lockKey);
			if (cacheVal != null && value.equals(cacheVal)) {
				shardedJedis.del(lockKey);
				return true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}
	
}
